<?php

namespace plugin\filesystem\api;

use plugin\admin\app\model\Option;

class Config
{

    /**
     * Option表的name字段值
     */
    const SETTING_OPTION_NAME = 'filesystem_setting';

    /**
     * Option表模版前缀
     */
    const TEMPLATE_OPTION_PREFIX = 'email_template_';

    public static function getDefaultConfig(){
        return [
            'default' => 'public',
            'max_size' => 1024*1024*10,
            'ext_yes' => '',
            'ext_no' => '',
            'storage' => [
                'public' => ['path'=>'/uploads','url'=>'http://'.\request()->host().'/uploads'],
                'local' => ['path'=>'','url'=>'http://'.\request()->host()],
                'oss' => ['access_id'=>'','access_key' => '','bucket' => '','endpoint' => '','ssl' => true,'isCName' => true,'url'=>'http://'.\request()->host()],
                's3'=>[
                    'key' => '',
                    'secret' => '',
                    'region' => '',
                    'version' => '',
                    'bucket_endpoint' => false,
                    'use_path_style_endpoint' => false,
                    'endpoint' => '',
                    'bucket_name' => '',
                    'url'=> 'http://'.\request()->host()
                ],
                'cos'=>[
                    'region'=> '',
                    'app_id' => '',
                    'secret_id' => '',
                    'secret_key' => '',
                    'bucket' => '',
                    'signed_url' => false,
                    'read_from_cdn' => false,
                    'url'=> 'http://'.\request()->host()
                ],
                'qiniu'=>[
                    'accessKey'=> '',
                    'secretKey' => '',
                    'bucket' => '',
                    'domain' => '',
                    'url'=> 'http://'.\request()->host()
                ],
                'ftp'=>[
                    'host'=> '',
                    'username' => '',
                    'password' => '',
                    'port' => 21,
                    'root' => '',
                    'passive' => false,
                    'ssl' => false,
                    'url'=> 'http://'.\request()->host()
                ]

            ]
        ];
    }

    /**
     *
     * @return array
     */
    public static function getDbConfig(){
        $config = Option::where('name', static::SETTING_OPTION_NAME)->value('value');
        $setConfig = $config ? json_decode($config, true) : self::getDefaultConfig();
        return [
            'default' => $setConfig['default']??'public',
            'max_size' => $setConfig['max_size'] ?? 1024 * 1024 * 10, //单个文件大小10M
            'ext_yes' => empty($setConfig['ext_yes']) ? []: explode(',',$setConfig['ext_yes']), //允许上传文件类型 为空则为允许所有
            'ext_no' => empty($setConfig['ext_no']) ? []: explode(',',$setConfig['ext_no']), // 不允许上传文件类型 为空则不限制
            'storage' => [
                'public' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\LocalAdapterFactory::class,
                    'root' => public_path().($setConfig['storage']['public']['path'] ?? ''),
                    'url' => $setConfig['storage']['public']['url'] ?? '' // 静态文件访问域名
                ],
                'local' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\LocalAdapterFactory::class,
                    'root' => runtime_path().($setConfig['local']['path'] ?? ''),
                    'url' => $setConfig['storage']['local']['url'] ?? '' // 静态文件访问域名
                ],
                'ftp' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\FtpAdapterFactory::class,
                    'host' => $setConfig['storage']['ftp']['host'] ?? '',
                    'username' => $setConfig['storage']['ftp']['username'] ?? '',
                    'password' => $setConfig['storage']['ftp']['password'] ?? '',
                    'url' => $setConfig['storage']['ftp']['url'] ?? '', // 静态文件访问域名
                    'port' => $setConfig['storage']['ftp']['port'] ?? 21,
                    'root' => $setConfig['storage']['ftp']['root'] ?? '',
                    'passive' => $setConfig['storage']['ftp']['passive'] ?? false,
                    'ssl' => $setConfig['storage']['ftp']['ssl'] ?? false,
                    // 'timeout' => 30,
                    // 'ignorePassiveAddress' => false,
                    // 'timestampsOnUnixListingsEnabled' => true,
                ],
                'memory' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\MemoryAdapterFactory::class,
                ],
                's3' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\S3AdapterFactory::class,
                    'credentials' => [
                        'key' => $setConfig['storage']['s3']['key'] ?? '',
                        'secret' => $setConfig['storage']['s3']['secret'] ?? '',
                    ],
                    'region' => $setConfig['storage']['s3']['region'] ?? '',
                    'version' => $setConfig['storage']['s3']['version'] ?? '',
                    'bucket_endpoint' => $setConfig['storage']['s3']['bucket_endpoint'] ?? false,
                    'use_path_style_endpoint' => $setConfig['storage']['s3']['use_path_style_endpoint'] ?? false,
                    'endpoint' => $setConfig['storage']['s3']['endpoint'] ?? '',
                    'bucket_name' => $setConfig['storage']['s3']['bucket_name'] ?? '',
                    'url' => '' // 静态文件访问域名
                ],
                'oss' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\AliyunOssAdapterFactory::class,
                    'accessId' => $setConfig['storage']['oss']['accessId'] ?? '',
                    'accessSecret' => $setConfig['storage']['oss']['accessSecret'] ?? '',
                    'bucket' => $setConfig['storage']['oss']['bucket'] ?? '',
                    'endpoint' => $setConfig['storage']['oss']['endpoint'] ?? '',
                    'url' => $setConfig['storage']['oss']['url'] ?? '', // 静态文件访问域名
                    // 'timeout' => 3600,
                    // 'connectTimeout' => 10,
                    'isCName' => $setConfig['storage']['oss']['isCName'] ?? false,
                    // 'token' => null,
                    // 'proxy' => null,
                ],
                'qiniu' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\QiniuAdapterFactory::class,
                    'accessKey' => $setConfig['storage']['qiniu']['accessKey'] ?? '',
                    'secretKey' => $setConfig['storage']['qiniu']['secretKey'] ?? '',
                    'bucket' => $setConfig['storage']['qiniu']['bucket'] ?? '',
                    'domain' => $setConfig['storage']['qiniu']['domain'] ?? '',
                    'url' => $setConfig['storage']['qiniu']['url'] ?? '' // 静态文件访问域名
                ],
                'cos' => [
                    'driver' => \Shopwwi\WebmanFilesystem\Adapter\CosAdapterFactory::class,
                    'region' => $setConfig['storage']['cos']['region'] ?? '',
                    'app_id' => $setConfig['storage']['cos']['app_id'] ?? '',
                    'secret_id' => $setConfig['storage']['cos']['secret_id'] ?? '',
                    'secret_key' => $setConfig['storage']['cos']['secret_key'] ?? '',
                    // 可选，如果 bucket 为私有访问请打开此项
                    'signed_url' => $setConfig['storage']['cos']['signed_url'] ?? false,
                    'bucket' => $setConfig['storage']['cos']['bucket'] ?? '',
                    'read_from_cdn' => $setConfig['storage']['cos']['read_from_cdn'] ?? false,
                    'url' => $setConfig['storage']['cos']['url'] ?? '' // 静态文件访问域名
                    // 'timeout' => 60,
                    // 'connect_timeout' => 60,
                    // 'cdn' => '',
                    // 'scheme' => 'https',
                ],
            ],
        ];
    }
}